#!/usr/bin/env python3
# -*-coding:utf-8-*-

import docxtpl
from docx.shared import Mm
import sys
import os


# 要编辑的docx文档模板路径
docx_path = sys.path[0] + os.sep + r"input.docx"
# 要插入的图片1路径
image1_path = os.path.join(sys.path[0], 'temp', 'a1.jpg')
# 要插入的图片2路径
image2_path = os.path.join(sys.path[0], 'temp', 'b1.jpg')

# 创建docx对象
daily_docx = docxtpl.DocxTemplate(docx_path)
# 创建2张图片对象
insert_image1 = docxtpl.InlineImage(daily_docx, image1_path, width=Mm(30), height=Mm(50))
insert_image2 = docxtpl.InlineImage(daily_docx, image2_path, width=Mm(50), height=Mm(30))

# 渲染内容
context = {
    'name': '博得',
    'phone': 18510779999,
    'sex': '男',
    'id_card': 43250311111111,
    'card_type': '大陆身份证',
    'work_unit': '新东方烹饪学校',
    'education_degree': '小学生',
    'school': '蓝翔',
    'month': 2,
    'year': 2021,
    "portraitPicture": insert_image1,
    "signaturePicture": insert_image2,
    'created_y': 2021,
    'created_m': 1,
    'created_d': 13,
}

# 渲染docx
daily_docx.render(context)

# # 保存docx
# output_doc = 'new_docx.docx'
# daily_docx.save(output_doc)
#
# import os
# os.system('start ' + output_doc)

from Doc import show_output
show_output(daily_docx, open_type='__all__')